package com.yuanfeng.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.config.YFConfig;
import com.yuanfeng.commoms.constant.CommonStatusEnum;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.Json.JsonUtils;
import com.yuanfeng.commoms.util.MathUtil;
import com.yuanfeng.commoms.util.RedisUtil;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.order.dto.PaymentVocherDTO;
import com.yuanfeng.order.entity.OrderBaseEntity;
import com.yuanfeng.order.entity.OrderPaymentVocherEntity;
import com.yuanfeng.order.mapper.OrderPaymentVocherMapper;
import com.yuanfeng.order.service.OrderBaseService;
import com.yuanfeng.order.service.OrderPaymentVocherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class OrderPaymentVocherServiceImpl extends ServiceImpl<OrderPaymentVocherMapper, OrderPaymentVocherEntity>
    implements OrderPaymentVocherService{

    @Autowired
    private OrderBaseService orderBaseService;
    @Override
    public ResponseResult insertPaymentVocher(PaymentVocherDTO param) {
        String token = param.getToken();
        boolean isExist = RedisUtil.exists(YFConfig.YF_USER_TOKEN_USER + token);
        if (isExist) {
            //是数字订单 是order Id
            if (MathUtil.isNumeric(param.getOrderId())) {
                // 获取表里有没有数据
                OrderPaymentVocherEntity opaymentVocher = this.baseMapper.selectOne(new LambdaQueryWrapper<OrderPaymentVocherEntity>()
                        .eq(OrderPaymentVocherEntity::getOrderId, param.getOrderId()));
                if (null != opaymentVocher) {
                    // 获取前端穿的图片数组
                    List<String> imgs = param.getOrderPaymentVoucherImgs();
                    // 判断图片数量
                    if (param.getOrderPaymentVoucherImgs().size() > 5) {
                        return ResponseResult.fail(BizCodeEnume.UPLOAD_UP_TO_5_PHOTOS);
                    } else {
                        String s = JsonUtils.toString(imgs);
                        param.setJsonImgs(s);
                        Integer integer = this.baseMapper.updatePaymentVocher(param);
                        if (integer > 0) {
                            return ResponseResult.success(CommonStatusEnum.SUCCESS.getCode(), BizCodeEnume.UPLOAD_SUCCESS.getMsg(), integer);
                        }
                    }
                } else {
                    if (param.getOrderPaymentVoucherImgs().size() > 5) {
                        return ResponseResult.fail(BizCodeEnume.UPLOAD_UP_TO_5_PHOTOS);
                    }
                    String jsonImgs = JsonUtils.toString(param.getOrderPaymentVoucherImgs());
                    param.setJsonImgs(jsonImgs);
                    Integer integer = this.baseMapper.insertPaymentVocher(param);
                    if (integer > 0) {
                        return ResponseResult.success(BizCodeEnume.UPLOAD_SUCCESS.getMsg());
                    }
                }
                //不是数字订单 payNum
            } else {
                String orderId = orderBaseService.getOne(new LambdaQueryWrapper<OrderBaseEntity>()
                        .eq(OrderBaseEntity::getPaymentNumber, param.getOrderId()).last("limit 1")).getOrderId();
                param.setOrderId(orderId);
                String jsonImgs = JsonUtils.toString(param.getOrderPaymentVoucherImgs());
                param.setJsonImgs(jsonImgs);
                Integer integer = this.baseMapper.insertPaymentVocher(param);
                if (integer > 0) {
                    return ResponseResult.success(BizCodeEnume.UPLOAD_SUCCESS.getMsg());
                }
            }
        } else {
            return ResponseResult.success(BizCodeEnume.TOKEN_IS_INVALID.getMsg());
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult querkVocherImages(PaymentVocherDTO paymentVocherDTO) {
        boolean isExist = RedisUtil.exists(YFConfig.YF_USER_TOKEN_USER + paymentVocherDTO.getToken());
        if (isExist) {
            OrderPaymentVocherEntity paymentVocherPo = this.baseMapper.selectOne(new LambdaQueryWrapper<OrderPaymentVocherEntity>()
                    .eq(OrderPaymentVocherEntity::getOrderId, paymentVocherDTO.getOrderId()));
            if (null != paymentVocherPo) {
                return ResponseResult.success(paymentVocherPo);
            } else {
                paymentVocherPo = new OrderPaymentVocherEntity();
                paymentVocherPo.setReviewStatus(0);
                return ResponseResult.success(BizCodeEnume.NO_AUDIT_INFORMATION.getMsg(), paymentVocherPo);
            }
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }
}




